import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.geom.*;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;


class GBall extends Ball implements Drawable {
   private float radius;
   private Color color = Color.BLUE;
   private Ellipse2D.Float shape = null;
   private String massval;

   public GBall (float mass, Vector2D position, Vector2D speed, 
                  float radius){
      super(mass, position, speed);
      massval = ""+mass;
      this.radius = radius;
      setCrossSection(6*Math.PI*radius);
      shape = new Ellipse2D.Float((float)getX()-radius, (float)getY()-radius, 2*radius, 2*radius);
   }
   /**
    * dibuja en pantalla el objeto ball. recupera el valor de masa para desplegarlo
    * en pantalla, dentro de un rectangulo del mismo color que la bola. Este cambia
    * de tamaño dinamicamente para adaptarse al tamaño del string que representa
    * la masa.
    * @param g
    */
   public void draw (Graphics2D g){
      AffineTransform at = new AffineTransform();
      at.setToTranslation(-0.095,-0.05);
      at.scale(0.011, -0.011);
      massval = ""+get_mass();
      AttributedString styledText = new AttributedString(massval);
      styledText.addAttribute(TextAttribute.FOREGROUND, new Color(0,255,0));
      styledText.addAttribute(TextAttribute.FAMILY,Font.DIALOG_INPUT);
      styledText.addAttribute(TextAttribute.WEIGHT,TextAttribute.WEIGHT_LIGHT);
      styledText.addAttribute(TextAttribute.WIDTH,TextAttribute.WIDTH_CONDENSED);
      styledText.addAttribute(TextAttribute.TRANSFORM, at);
      AttributedCharacterIterator iter = styledText.getIterator();
      g.setColor(color);
      g.fill(new Rectangle2D.Float((float)(getX()-0.08),(float) (getY()-0.08),(float) ((massval.length()-1)*0.065+0.05),(float)0.13));
      shape.setFrame((float)getX()-radius, (float)getY()-radius, 2*radius, 2*radius);
      g.fill(shape);
      g.setColor(color.YELLOW);
      g.drawString(iter, (float)getX(),(float)getY());
   }

   public boolean contains (double x, double y){
      return shape.contains(x,y);
   }
   public void dragTo(double x, double y){
      setPosition(x,y);
   }
   public void setSelected (){
      color = Color.RED;
   }
   public void setReleased() {
      color = Color.BLUE;
   }
}